// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Eggmobile Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 81 : TYPE_EGGMOBILE


private alias saveRAM[494] : saved.bossGameMode //7B8 SData.bin
private alias saveRAM[495] : saved.bossType //7BC SData.bin
private alias saveRAM[496] : saved.BossMusic //7C0 SData.bin
private alias saveRAM[497] : saved.Character //7C4 SData.bin
private alias saveRAM[498] : saved.TimeLimit //7C8 SData.bin
private alias saveRAM[499] : saved.superMode //7CC SData.bin

// Static Values

// Tables

event ObjectMain
	switch object.state
	case 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -24, -96, 24, 96, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		next
		break
	case 1
		object.priority = PRIORITY_ACTIVE
		object.animation = 2
		object.value0 = 1
		object.yvel = -0x18000
		object.state++
		break
	case 2
		object.ypos += object.yvel
		object.value4++
		if object.value4 >= 52
			object.value2 = 1
			object.xvel = 0x18000
			object.yvel = -0x1800
			object.state++
		end if
		break
	case 4
		object.value5++
		temp0 = object.value5
		temp0 &= 7
		if temp0 == 4
			Rand(temp0, 96)
			temp0 -= 48
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 48)
			temp1 -= 24
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
			object[tempObjectPos].drawOrder = 4
			if object.value7 < 256
				PlaySfx(SfxName[Explosion], 0)
			end if
		end if
		if object.value7 < 256
			object.value4++
			if object.value4 >= 30
				PlaySfx(SfxName[Boss Hit], 0)
				object.yvel = 0x6000
				object.value4 = 0
			end if
		end if
	case 3
		object.xpos += object.xvel
		object.ypos += object.yvel
		temp0 = stage.newXBoundary2
		temp0 += 212
		temp0 <<= 16
		if object.xpos >= temp0	
			object.visible = 0
			if saved.bossGameMode == 1
				object.state = 10
			else
				stage.timeEnabled = 0
				object.value7 += 4
				if stage.activeList != BONUS_STAGE
					music.volume -= 2
				endif
				SetScreenFade(0, 0, 0, object.value7)
				if object.value7 == 384
					if stage.debugMode == 0
						if options.stageSelectFlag == 0
							CallNativeFunction2(SetLeaderboard, 19, player.score)
						end if
					end if
					if options.gameMode == 1
						arrayPos1 = options.saveSlot
						arrayPos1 <<= 3
						arrayPos1 += 4
						saveRAM[arrayPos1] = 20
						WriteSaveRAM()
					end if
					if stage.activeList == BONUS_STAGE
						vs.rings1P = object[0].value0
						vs.time1P = stage.seconds
						vs.time1P *= 100
						temp2 = stage.minutes
						temp2 *= 0x1770
						vs.time1P += temp2
						vs.time1P += stage.milliSeconds
						vs.items1P = object[0].value37
						vs.speed = object[0].speed
						stage.timeEnabled = true
						stage.listPos++
					else
						stage.activeList = PRESENTATION_STAGE
						stage.listPos = 1
					endif
					LoadStage()
				end if
			end if
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object[currentPlayer].xpos >= object.value6
				CheckEqual(object[currentPlayer].state, PlayerObject_HandleGround)
				temp0 = checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_HandleAir)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_HandleRolling)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_RollingJump)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].state, PlayerObject_Blank)
				temp0 |= checkResult
				if temp0 == 1
					object[currentPlayer].state = PlayerObject_Blank
					object[currentPlayer].direction = FLIP_NONE
					object[currentPlayer].controlMode = -1
					if object[currentPlayer].gravity == GRAVITY_GROUND
						object[currentPlayer].xvel = 0
						object[currentPlayer].speed = 0
						if object.state == 3
							object[currentPlayer].animation = ANI_LOOKINGUP
						else
							object[currentPlayer].animation = ANI_STOPPED
						end if
					else
						object[currentPlayer].speed = object[currentPlayer].xvel
						object[currentPlayer].yvel += 0x3800
					end if
				else
					if object[currentPlayer].state == PlayerObject_KnuxGlideSlide
						object[currentPlayer].speed = 0
					end if
				end if
			end if
		next
		break
	case 10
				temp0 = tileLayer[0].xsize
				temp0 <<= 7
				stage.newXBoundary2 = temp0
				temp1 = stage.curYBoundary2
				temp1 += 2560
				temp1 <<= 16
				stage.deathBoundary = temp1
				stage.timeEnabled = 0
				
				foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
					object[currentPlayer].state = PlayerObject_HandleGround
					object[currentPlayer].right = 1
				next
				object.type = TypeName[Blank Object]
		break
	case 5
		object.xpos = object[+10].xpos
		object.ypos = object[+10].ypos
		if object[+1].type == TypeName[Blank Object]
			object.type = TypeName[Blank Object]
		end if
		break
	end switch
	if object.state == 3
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -20, -16, 20, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				CallFunction(PlayerObject_CheckHit)
				if checkResult == 1
					object.state = 4
					object.animation = 3
					PlaySfx(SfxName[Boss Hit], 0)
					object.value4 = 0
					if stage.debugMode == 0
						if currentPlayer == 0
							CallNativeFunction2(SetAchievement, 10, 100)
						end if
					end if
				end if
			end if
			if object.state < 10
				temp0 = object[currentPlayer].collisionRight
				temp0 <<= 16
				temp0 += object[currentPlayer].xpos
				temp1 = stage.curXBoundary2
				temp1 <<= 16
				if temp0 > temp1
					object[currentPlayer].xvel = 0
					object[currentPlayer].speed = 0
					object[currentPlayer].xpos = temp1
					temp0 = object[currentPlayer].collisionRight
					temp0 <<= 16
					object[currentPlayer].xpos -= temp0
				end if
			end if
		next
	end if
end event


event ObjectDraw
if object.state != 10
	switch object.animation
	case 0
		DrawSpriteFX(16, FX_FLIP, object.xpos, object.ypos)
		DrawSpriteFX(17, FX_FLIP, object.xpos, object.ypos)
		break
	case 1
		DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object.animationTimer
		temp0 >>= 2
		temp0 += 1
		object.animationTimer++
		object.animationTimer &= 7
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 3
		temp0 = object.animationTimer
		temp0 >>= 2
		temp0 += 3
		object.animationTimer++
		object.animationTimer &= 7
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	end switch
	switch object.value0
	case 0
		DrawSpriteFX(5, FX_FLIP, object.xpos, object.ypos)
		break
	case 1
		object.value1++
		if object.value1 >= 60
			object.value0 = 2
		else
			temp0 = object.value1
			temp0 /= 20
			temp0 += 5
			DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		end if
		break
	case 2
		break
	end switch
	if object.value2 != 0
		temp0 = object.value3
		temp0 /= 3
		temp0 += 8
		object.value3++
		if object.value3 > 21
			object.value3 = 18
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
	end if
endif
end event


event ObjectStartup
	if saveRAM[495] == 0
		LoadSpriteSheet("Global/Eggman.gif")
	else
		LoadSpriteSheet("Global/EggRobo.gif")
	endif
	SpriteFrame(-28, -32, 64, 21, 131, 172)
	SpriteFrame(-28, -32, 64, 56, 131, 58)
	SpriteFrame(-28, -32, 64, 56, 66, 1)
	SpriteFrame(-28, -32, 64, 56, 1, 172)
	SpriteFrame(-28, -32, 64, 56, 66, 172)
	SpriteFrame(-20, 16, 40, 21, 131, 115)
	SpriteFrame(-20, 8, 48, 19, 172, 115)
	SpriteFrame(-20, 8, 40, 16, 172, 135)
	SpriteFrame(34, 1, 10, 9, 196, 2)
	SpriteFrame(33, 0, 15, 11, 196, 12)
	SpriteFrame(34, -1, 22, 14, 213, 1)
	SpriteFrame(34, -8, 32, 26, 213, 16)
	SpriteFrame(34, -1, 22, 14, 213, 1)
	SpriteFrame(34, -8, 32, 26, 213, 16)
	SpriteFrame(34, 1, 10, 9, 196, 2)
	SpriteFrame(33, 0, 15, 11, 196, 12)
	SpriteFrame(-28, -32, 27, 21, 131, 172)
	SpriteFrame(-28, -11, 64, 35, 131, 193)
	foreach (TypeName[Eggmobile], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].direction = FLIP_X
		if object[arrayPos0].propertyValue == 1
			object[arrayPos0].state = 5
			object[arrayPos0].animation = 1
			object[arrayPos0].value0 = 2
		end if
	next
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
